package com.g1.screens.match3;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Path;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import com.g1.screens.enumerations.GameStates;
import com.g1.screens.services.BlockService;
import com.g1.screens.services.Match3Service;


public class Match3 extends BasicGameState   {
	
	private Image background;
	private Image leRest;
	
	private Image playerStatus;
	private Image ennemyStatus;
	private Image status;
	
	private Path pathPlayerStatus;
	private Path pathEnnemyStatus;
	
	private Vector2f vec;
	private List<Vector2f> points;
	private int index;
	private boolean toRight = true;
	private boolean toUp = true;
	
	private BlockService blockService;
	private Match3Service match3service;
	private float angle = 0;
	
	private int directionV = 0;
	
	private Sound gong;
	private Sound wrong;
	
	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		match3service = new Match3Service();// TODO Auto-generated method stub
		
		background = new Image("data/background.png");
		leRest  = new Image("data/lereste.png");
		Image fighters = new Image("data/fighters.png");
		playerStatus = fighters.getSubImage(140, 0, 127, 133);
		ennemyStatus = fighters.getSubImage(268, 0, 127, 133);
		status = fighters.getSubImage(0, 0, 139, 141);
		
		pathPlayerStatus = new Path(20, 400);
		pathPlayerStatus.curveTo(30, 440, 1, 2, 3, 4);
		pathPlayerStatus.lineTo(35, 420);
		pathEnnemyStatus = new Path(880, 400);
		points = new ArrayList<Vector2f>();
		for(int i = 0; i<pathPlayerStatus.getPoints().length-1 ;i=i+2){
			points.add(new Vector2f(pathPlayerStatus.getPoints()[i],pathPlayerStatus.getPoints()[i+1]));
		}
		vec = points.get(0);
		blockService = new BlockService();
		
		gong = new Sound("res/gong.wav");
		wrong = new Sound("res/data/sound/wrong.wav");
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		// TODO Auto-generated method stub
		g.drawImage(background, 0, 0);
		g.drawImage(leRest, 0, 0);
//		blockService.render(container, game, g);
//		g.drawString(blockService.getAnimationList(), 15, 40);
//		g.drawString(blockService.getCurrentAnimation2Play().toString(), 15, 80);
		
		g.drawImage(playerStatus,28,400);
		status.rotate(angle);
		g.drawImage(status,20,400);
		
		g.drawImage(ennemyStatus,890,400);
		g.drawImage(status,880,400);
		
		match3service.render(container, game, g);
		g.drawString(match3service.getState().toString(), 15, 80);
		
		
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		
		
		
		calCulateNextPoint(delta, 100, 54);
		angle=0.01f*delta;
		
		
		match3service.update(container, game, delta);
		
//		switch(blockService.getCurrentAnimation2Play()) {
//			case IDLE:
//			
//			if(container.getInput().isMousePressed(0)){
//				System.out.println("click");
//				Block b = blockService.retrievedBlockFromClick(container.getInput().getMouseX(), container.getInput().getMouseY());			
//				System.out.println("b="+(b!=null?b.getType():null));
//				if(b == null) { 
//					blockService.clearSelectedBlock();
//				} else {
//					// ******************************************
//					
//							// there is a block found
//							if(blockService.getFirstSelectedBlock()==b) {
//								blockService.clearSelectedBlock();
//							} else if(blockService.getFirstSelectedBlock()!=null){
//								// second block found
//								if(blockService.isContiguousBlock(blockService.getFirstSelectedBlock(), b)) {
//									System.out.println("OK CONTIGUS!");
//									blockService.setSecondSelectedBlock(b);
//									if(blockService.calculateMove(blockService.getFirstSelectedBlock(), blockService.getSecondSelectedBlock())){
//										System.out.println("success!");
//									} else {
//										System.out.println("bad move - no selection");
//									};
//								} else {
//									// we concidere the user wants another block to be selected as the first one
//									blockService.clearSelectedBlock();
//									blockService.setFirstSelectedBlock(b);
//								}
//							} else {	
//								 // first block found no conditions
//								blockService.setFirstSelectedBlock(b);
//							} 
//					
//					// ******************************************
//				}
//			}
//			break;
//			
//			//SWITCH ANIMATION
//			case SWITCH : 
//				int direction = 1;
//				Block b1 = blockService.getFirstSelectedBlock();
//				Block b2 = blockService.getSecondSelectedBlock();
//				if(b1.getColumn()==b2.getColumn()) {
//					// blocks are on the same column (verticals)
//					direction = (b1.getRow()<b2.getRow()?1:-1);
//					System.out.println("direction = "+direction);
//					b1.moveDeltaY(delta * direction * 0.1f); 
//					System.out.println(b1.getType().getName() +" to Y b1 => "+delta * direction * 0.1f);
//					b2.moveDeltaY(delta * -direction * 0.1f);
//					System.out.println(b2.getType().getName() +" to Y b2 => "+delta * -direction * 0.1f);
//					if(b1.isReachPosition()  || b2.isReachPosition()){
//						System.out.println("D O N E VERT");
//						b1.resetDelta();
//						b2.resetDelta();
//						blockService.getNextAnimation2Play();
//						blockService.clearSelectedBlock();
//					}
//				} else
//				if(b1.getRow()==b2.getRow()) {
//					// blocks are horizontals
//					direction = (b1.getColumn()<b2.getColumn()?+1:-1);
//					b1.moveDeltaX(delta *  direction * 0.1f);
//					b2.moveDeltaX(delta * -direction * 0.1f);
//					if(b1.isReachPosition() || b2.isReachPosition()){
//						System.out.println("D O N E HORI");
//						b1.resetDelta();
//						b2.resetDelta();
//						blockService.getNextAnimation2Play();
//						
//					}
//				} else {
//					throw new SlickException("This case should not appear - please debug!");
//				}
// 
//				
//			break;
//			case GOODMOVE : 
//				System.out.println("GOOD MOVE");
//				gong.play(1.0f,0.6f);
//				blockService.getNextAnimation2Play();
//				break;
//			case SWITCHBACK : 
//				System.out.println("Switch back");
//				wrong.play(1.0f,0.6f);
//				Block source = blockService.getFirstSelectedBlock();
//				Block destination = blockService.getSecondSelectedBlock();
//				blockService.switchAction(source, destination);
//				blockService.getNextAnimation2Play();
//			break;
//			case FALL : 
//				System.out.println("FALL");
//				blockService.clearSelectedBlock();
//				blockService.getNextAnimation2Play();
//				break;
//			
//			default : blockService.getNextAnimation2Play();
//			break;
//	}
		
		
		
		
		if(container.getInput().isKeyPressed(Keyboard.KEY_A)){
			System.out.println(vec);
		}
		if(container.getInput().isKeyPressed(Keyboard.KEY_ESCAPE)){
			container.exit();
		}
		
	}

	@Override
	public int getID() {
		return GameStates.MATCH3.getId();
	}

	private void calCulateNextPoint(int delta, int oscX, int oscY){
		if(toRight) {
			if(vec.getX()<=oscX) {
				vec.set(vec.getX()+0.02f*delta,vec.getY());
			} else {
				toRight = false;
			}
		} else if(vec.getX()>10){
			vec.set(vec.getX()-0.02f*delta,vec.getY());
		} else {
			toRight = true;
		}
		
		///
		
		if(toUp) {
			if(vec.getY()<=oscY) {
				vec.set(vec.getX(),vec.getY()+0.005f*delta);
			} else {
				toUp = false;
			}
		} else if(vec.getY()>10){
			vec.set(vec.getX(),vec.getY()-0.005f*delta);
		} else {
			toUp = true;
		}
	}
	
	private Vector2f getNextPoint2(int delta){
		int i = index;
		if(index<points.size()) {
			index++;
		} else {
			index = 0;
			i = 0;
		}
		return points.get(i);
	}

}
